package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.DisposeLabel;
import com.css.fxfzaqbz.modules.data.entity.DisposeSubtitle;
import com.css.fxfzaqbz.modules.data.entity.ExpertManagement;
import com.css.fxfzaqbz.modules.data.repository.ExpertManagementRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ExpertManagementRepositoryImpl implements ExpertManagementRepository {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(List<ExpertManagement> expertManagements) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(expertManagements.toArray());
        String sql = "INSERT INTO sys_expert_management (id,expert_id,expert_name,dept,phone,del_flag,create_user,create_time,province_id) values(:id,:expertId,:expertName,:dept,:phone,:delFlag,:createUser,:createTime,:provinceId)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public Map<String, Object> getAll(int curPage, int pageSize, ExpertManagement expertManagement) {
        StringBuilder sql = new StringBuilder(" select * from sys_expert_management ");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '" + YNEnum.N.toString() + "'");
        if (!PlatformObjectUtils.isEmpty(expertManagement.getExpertName())) {
            whereSql.append(" and expert_name like '%" + expertManagement.getExpertName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(expertManagement.getProvinceId())) {
            whereSql.append(" and province_id = '" + expertManagement.getProvinceId() + "'");
        }
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        StringBuilder wholeSql = sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(wholeSql.toString(), new BeanPropertyRowMapper<>(ExpertManagement.class)).size();
        wholeSql.append(limitSql);
        List<ExpertManagement> expertManagements = jdbcTemplate.query(wholeSql.toString(), new BeanPropertyRowMapper<>(ExpertManagement.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", expertManagements);
        return map;
    }

    @Override
    public void delete(List<String> ids) {
        String sql = "update sys_expert_management set del_flag = '" + YNEnum.Y + "' where id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }
}
